#coding=utf-8
'''
Created on 2013-9-8

@author: Ben

资源管理
'''
from ..common import BasicThread, AIConfig, Logger
from ..role import RoleData, FunctionData
import FeatList
import Market
import NationFeed
import OnlineReward
import ResourceData
import Worship
import time

resourceManager = None
featListGetInfoRequest = None
worshipGetInfoRequest = None
onlineRewardGetInfoRequest = None
marketGetInfoRequest = None
nationFeedGetInfoRequest = None

def featListGetInfoCallback():  
    spareBoxNum = ResourceData.featBoxNum + ResourceData.getNextFeatListBoxNum() - AIConfig.featlist_box_upper_bound
    if spareBoxNum > ResourceData.featBoxNum:
        spareBoxNum = ResourceData.featBoxNum
    while spareBoxNum > 0:
        spareBoxNum -= 1
        FeatList.getFeatListBoxReward()
        time.sleep(1)

def worshipListGetInfoCallback():  
    #判断是否需要祭祀
    if RoleData.food > AIConfig.worship_start_food or ResourceData.freeWorshipNum <= 0:
        return
    #祭祀次数
    worshipNum = AIConfig.worship_consecutive_times
    if ResourceData.freeWorshipNum < AIConfig.worship_consecutive_times:
        worshipNum = ResourceData.freeWorshipNum
    while worshipNum > 0:
        worshipNum -= 1
        Worship.worship(3)
        time.sleep(1)

def onlineRewardGetInfoCallback():
    #需要领取的整点在线奖励次数
    onlineRewardNum = 1
    if AIConfig.online_reward_all_allowed or onlineRewardNum > RoleData.onlineRewardNum:
        onlineRewardNum = RoleData.onlineRewardNum
    #领取整点在线奖励
    while onlineRewardNum > 0:
        onlineRewardNum -= 1
        OnlineReward.getOnlineReward()
        time.sleep(0.5)

def marketGetInfoCheck():
    return ResourceData.marketNum >= AIConfig.market_num_upper_bound

def marketGetInfoCallback():
    Logger.log('集市购买次数:'+str(ResourceData.marketNum), output=True)
    #次数不够直接返回
    if ResourceData.marketNum <= 0:
        return
    #获取当前价值最高的商品id和类型
    if len(ResourceData.marketGoods) > 0:
        bestGoods = max(ResourceData.marketGoods)
        #次数爆满
        if ResourceData.marketNum >= AIConfig.market_num_upper_bound:
            Logger.log('集市次数爆满', output=True)
            ResourceData.marketNum -= 1
            Market.marketBuyGoods(bestGoods.id)
        #发现珍品
        elif bestGoods.itemType in AIConfig.market_precious_goods:
            Logger.log('发现珍品', output=True)
            ResourceData.marketNum -= 1
            Market.marketBuyGoods(bestGoods.id)
        else:
            Logger.log('没有值得购买的商品', output=True)

def nationFeedGetInfoCallback():
    Logger.log('当前军资数量:'+str(ResourceData.nationFeedNum), output=True)
    while ResourceData.nationFeedNum >= AIConfig.nation_feed_upper_bound:
        ResourceData.nationFeedNum -= 1
        NationFeed.getNationFeed()
        time.sleep(0.5)

def start():
    global resourceManager
    global featListGetInfoRequest
    global worshipGetInfoRequest
    global onlineRewardGetInfoRequest
    global marketGetInfoRequest
    global nationFeedGetInfoRequest
    if resourceManager == None:
        resourceManager = ResourceManager(name="resourceManager")
    featListGetInfoRequest = FeatList.FeatListGetInfoRequest(None,  featListGetInfoCallback, 30, 5)
    worshipGetInfoRequest = Worship.WorshipGetInfoRequest(None, worshipListGetInfoCallback, 30, 5)
    onlineRewardGetInfoRequest = OnlineReward.OnlineRewardGetInfoRequest(None, onlineRewardGetInfoCallback, 30, 5)
    marketGetInfoRequest = Market.MarketGetInfoRequest(marketGetInfoCheck, marketGetInfoCallback, 30, 5)
    nationFeedGetInfoRequest = NationFeed.NationFeedGetInfoRequest(None, nationFeedGetInfoCallback, 30, 5)
    resourceManager.startThread()
    
def pause():
    global resourceManager
    if resourceManager != None:
        resourceManager.pauseThread()

class ResourceManager(BasicThread.BasicThread):
    
    def callback(self):
        time.sleep(20)
        #功勋榜
        global featListGetInfoRequest
        if FunctionData.func32 == 1 and featListGetInfoRequest.run():
            return
        
        #整点在线奖励
        global onlineRewardGetInfoRequest
        if FunctionData.func39 == 1 and onlineRewardGetInfoRequest.run():
            return
        
        #集市
        global marketGetInfoRequest
        if FunctionData.func27 == 1 and marketGetInfoRequest.run():
            return
        
        #祭祀
        global worshipGetInfoRequest
        if FunctionData.func16 == 1 and worshipGetInfoRequest.run():
            return
        
        #军资
        global nationFeedGetInfoRequest
        if FunctionData.func53 == 1 and nationFeedGetInfoRequest.run():
            return